import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:tiktokadult/hj_model/acg/CartoonMoreList.dart';
import 'package:tiktokadult/hj_model/cartoon_media_info.dart';
import 'package:tiktokadult/hj_model/home/home_tabs_entity.dart';
import 'package:tiktokadult/hj_model/home/module_detail_model.dart';
import 'package:tiktokadult/hj_utils/api_service/acg_service.dart';
import 'package:tiktokadult/hj_utils/api_service/vid_service.dart';

import '../../tools_base/ad_insert_manager.dart';
import '../../tools_base/global_store/local_ads_info_store.dart';

class CartoonSubSectionLogic extends GetxController {
  CartoonSubSectionLogic({this.tagData});

  final ModuleData? tagData; //模块model

  List<AllSection>? sectionList;
  int sectionPage = 1; //专题的分页
  int likePage = 1; //猜你喜欢page
  int likeSize = 12; //猜你喜欢size
  double eightHeight = (Get.width - 30) / 2.5 * 216 / 133; //八竖布局高度
  RefreshController refrshCtr = RefreshController();
  PreferencesModel? preferenceInfo; //猜你喜欢
  List<CartoonMediaInfo> preferenceList = []; //猜你喜欢列表
  RxInt selectIndex = 0.obs; //猜你喜欢排序
  bool likeLoading = false;
  late StreamSubscription topAction;
  late ScrollController scl = ScrollController()
    ..addListener(() {
      // if (preferenceInfo != null) {
      //   if (scl.offset >= likeDy) {
      //     tabTopStore.acgTopNotification(true, id: tagData?.id); //切换tab
      //   } else {
      //     tabTopStore.acgTopNotification(false, id: tagData?.id); //切换tab
      //   }
      // }
    });
  double likeDy = 0; //猜你喜欢-顶部

  @override
  void onReady() {
    super.onReady();
    loadData(); //获取
    // topAction = eventBus.on<ACGIconTop>((event) {
    //   if (event.id == tagData?.id) scl.jumpTo(0);
    // });
  }

  @override
  onClose() {
    topAction.cancel();
    super.onClose();
  }

  loadData({int pageNum = 1}) async {
    if (preferenceInfo == null) {
      loadSectionTypeData(pageNum: pageNum);
    } else {
      loadPreferences(pageNum: pageNum);
    }
  }

  loadMoreData() {
    if (preferenceInfo == null) {
      loadSectionTypeData(pageNum: sectionPage + 1);
    } else {
      loadPreferences(pageNum: likePage + 1);
    }
  }

  //获取专题数据
  loadSectionTypeData({int pageNum = 1}) async {
    sectionList ??= [];
    ModuleDetailModel? res = await VidService.getModuleDetail(
      tagData?.id ?? '',
      pageNumber: pageNum,
      pageSize: 5,
    );

    if (res != null) {
      if (pageNum == 1) {
        preferenceInfo = null;
        sectionList?.clear();
        preferenceList.clear();
      }
      sectionPage = pageNum;
      //过滤空数据
      res.allSection?.removeWhere((element) => element.allMediaInfo?.isEmpty ?? true);
      sectionList?.addAll(res.allSection ?? []);
      sectionList?.removeWhere((element) => element.isAdsArr());
      AdInsertManager.section(sectionList ?? [], getAdvByTypeSync(5), adGap: 2);
      if (res.preferencesModel != null) {
        if (sectionList?.last.isAdsArr() == true) {
          sectionList?.removeLast();
        }
        preferenceInfo = res.preferencesModel;
        selectIndex.value = preferenceInfo?.defaultSortType ?? 0;
        preferenceList.addAll(preferenceInfo?.list ?? []);
        likePage = preferenceInfo?.pageNumber ?? 1;
        likeSize = preferenceInfo?.pageSize ?? 12;
        preferenceInfo?.hasNext == true ? refrshCtr.loadComplete() : refrshCtr.loadNoData();
        refrshCtr.refreshCompleted();
        if (preferenceInfo != null) likeDy = scl.offset;
        print('ACGSectionLogic == init likeDy = $likeDy');
      } else {
        res.hasNext == true ? refrshCtr.loadComplete() : refrshCtr.loadNoData();
        refrshCtr.refreshCompleted();
      }
      update();
    }
  }

  //猜你喜欢
  loadPreferences({int pageNum = 1}) async {
    CartoonMoreListModel? res = await ACGService.getPreferences(
      pageNum,
      likeSize,
      type: 1,
      sortType: selectIndex.value,
      sId: preferenceInfo?.sectionID ?? '',
    );
    likeLoading = false;
    likePage = pageNum;
    if (pageNum == 1) preferenceList.clear();
    preferenceList.addAll(res?.list ?? []);
    res?.hasNext == true ? refrshCtr.loadComplete() : refrshCtr.loadNoData();
    refrshCtr.refreshCompleted();
    update(['recommend']);
  }

  //获取
  onChangeRecomendSort(int index) {
    if (selectIndex.value != index) {
      selectIndex.value = index;
      likeLoading = true;
      update();
      loadPreferences();
    }
  }

  //获取猜你喜欢初始化位置
  onInitRecomendDy(double dy) => likeDy = dy;
}
